//
//  TSLoginManager.swift
//  TSProfitProject
//
//  Created by TMS on 2019/11/11.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit
import HandyJSON


struct TSLoginModel : HandyJSON {
    
    var token : String? = ""
    var username : String?
    var user_id = 0
    var id = 0
    var refree = 0
    var jinbi : CGFloat = 0
    var yongjin : CGFloat = 0
    var yingli : CGFloat = 0
    var zhongxin : CGFloat = 0
    var dengji = 0
    var liushui : Double = 0
    var liushui_now : Double = 0
    var item_num : Int64 = 0
    
}

public let kUserModel = "userModel"
fileprivate let sharedInstance = TSLoginManager()

class TSLoginManager: NSObject {

//    var userModel : TSLoginModel?
    
    var user_id = 0
    var token : String? = ""
    
    static var shared: TSLoginManager {
        return sharedInstance
    }

    private var userModel : TSLoginModel?
    
    var _usermodel : TSLoginModel? {
        get {
            return userModel
        }
    }
    
    func setUserModel(model:TSLoginModel?) {
        guard model != nil else {
            return
        }
        
        userModel = model
        UserDefaults.standard.set(model!.toJSON(), forKey: kUserModel)
        print("本地存储成功:\(String(describing: model!.toJSON()))")
    }
    
    func getUserModel() -> TSLoginModel? {
        let userDic = UserDefaults.standard.object(forKey: kUserModel) as? NSDictionary
        let usermodel = TSLoginModel.deserialize(from: userDic)
        
        if usermodel != nil {
            userModel = usermodel
            return _usermodel!
        } else {
            return nil
        }
        
    }
    
    func clearUserInfo() {
        UserDefaults.standard.removeObject(forKey: kUserModel)
        OYCountDownManager.sharedManager.removeAllSource()
    }

    fileprivate override init() { }
    
    
}
